﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public enum LogLevel
{
    /// <summary>
    /// 详细的信息, 通常只出现在诊断问题上
    /// </summary>
    Debug = 0,
    /// <summary>
    /// 确认一切按预期运行 
    /// </summary>
    Info = 1,
    /// <summary>
    /// 一个迹象表明, 一些意想不到的事情发生了, 或表明一些问题在不久的将来(例如。磁盘空间低”)。这个软件还能按预期工作。
    /// </summary>
    Warning = 2,
    /// <summary>
    /// 更严重的问题, 软件没能执行一些功能
    /// </summary>
    Error = 3,
    /// <summary>
    /// 一个严重的错误, 这表明程序本身可能无法继续运行
    /// </summary>
    Critical = 4,
}
public class Log
{
    private static readonly string _path;//日志所在路径
    private static readonly bool _isWriteConsole;//是否答应到控制台
    private static readonly bool _isOpenLog;//是否打开日志

    static Log()
    {
        _path = ConfigurationManager.ConnectionStrings["logPath"].ConnectionString;
        _isWriteConsole = true;
        _isOpenLog = true;
    }

    private static async Task Write(string msg, LogLevel level, DateTime dateTime)
    {
        if (!_isOpenLog) return;
        await Task.Run(() =>
        {
            string date = dateTime.ToString("yyyy-MM-dd ");//日期
            string time = dateTime.ToString("yyyy-MM-dd HH:mm:ss");//时间
            string url = _path + date + " - " + level + ".txt";
            StringBuilder sb = new StringBuilder(100);
            sb.AppendLine($"[{time}]:[{level}]:{msg}");
            msg = sb.ToString();

            if (_isWriteConsole) Console.Write(msg);//打印到控制台

            File.AppendAllText(url, msg);
        });
    }

    public static Task Info(string msg)
    {
        return Info(msg, DateTime.Now);
    }
    public static Task Info(string msg, DateTime dateTime)
    {
        return Write(msg, LogLevel.Info, dateTime);
    }

    public static Task Debug(string msg)
    {
        return Debug(msg, DateTime.Now);
    }
    public static Task Debug(string msg, DateTime dateTime)
    {
        return Write(msg, LogLevel.Debug, dateTime);
    }

    public static Task Warning(string msg)
    {
        return Warning(msg, DateTime.Now);
    }
    public static Task Warning(string msg, DateTime dateTime)
    {
        return Write(msg, LogLevel.Warning, dateTime);
    }

    public static Task Error(string msg)
    {
        return Error(msg, DateTime.Now);
    }
    public static Task Error(string msg, DateTime dateTime)
    {
        return Write(msg, LogLevel.Error, dateTime);
    }
    public static Task Error(string msg, Exception ex)
    {
        return Write("\n" + msg + "\n" + ex + "\n", LogLevel.Info, DateTime.Now);
    }

    public static Task Critical(string msg)
    {
        return Critical(msg, DateTime.Now);
    }
    public static Task Critical(string msg, DateTime dateTime)
    {
        return Write(msg, LogLevel.Critical, dateTime);
    }
    public static Task Critical(string msg, Exception ex)
    {
        return Write("\n" + msg + "\n" + ex + "\n", LogLevel.Info, DateTime.Now);
    }
}



